package com.fling.block;

import java.util.ArrayList;
import java.util.Arrays;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.fling.Constants;
import com.fling.Locator;
import com.fling.objects.Block;
import com.fling.objects.Hero;
import com.fling.observer.Event;
import com.fling.observer.Subject;

public class BlockRow implements Constants {

	private ArrayList<Block> row;
	private boolean passedHero;
	private Hero hero;
	private Subject subject;

	public BlockRow(Hero hero, Subject subject) {
		row = new ArrayList<Block>();
		passedHero = false;
		this.hero = hero;
		this.subject = subject;
	}

	public void buildStructuredRow(int[] order, float y_pos) {
		Block block = null;
		for (int i = 0; i < order.length; i++) {
			block = new Block(new Vector2(WALL_WIDTH * Locator.getxSpriteScale() + Locator.getBlockSpacing() * order[i], VIRTUAL_HEIGHT
					* Locator.getySpriteScale() + BLOCK_HEIGHT * Locator.getySpriteScale() * y_pos));
			row.add(block);
		}
	}

	public void buildRow(float y_pos) {
		Block block = null;
		ArrayList<Integer> positions = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3));
		int numOfBlocks = MathUtils.random(1, 3);
		for (int i = 0; i < numOfBlocks; i++) {
			int x_pos = MathUtils.random(0, positions.size() - 1);
			x_pos = positions.remove(x_pos);
			block = new Block(new Vector2(WALL_WIDTH * Locator.getxSpriteScale() + Locator.getBlockSpacing() * x_pos, VIRTUAL_HEIGHT
					* Locator.getySpriteScale() + BLOCK_HEIGHT * Locator.getySpriteScale() * y_pos));
			row.add(block);
		}
	}

	public Vector2 buildRowWithCoin(float y_pos) {
		Block block = null;
		ArrayList<Integer> positions = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3));
		int numOfBlocks = MathUtils.random(1, 3);
		for (int i = 0; i < numOfBlocks; i++) {
			int x_pos = MathUtils.random(0, positions.size() - 1);
			x_pos = positions.remove(x_pos);
			block = new Block(new Vector2(WALL_WIDTH * Locator.getxSpriteScale() + Locator.getBlockSpacing() * x_pos, VIRTUAL_HEIGHT
					* Locator.getySpriteScale() + BLOCK_HEIGHT * Locator.getySpriteScale() * y_pos));
			row.add(block);
		}
		int x_pos = MathUtils.random(0, positions.size() - 1);
		x_pos = positions.remove(x_pos);
		return new Vector2(WALL_WIDTH * Locator.getxSpriteScale() + Locator.getBlockSpacing() * x_pos + BLOCK_WIDTH * Locator.getxSpriteScale() / 2,
				VIRTUAL_HEIGHT * Locator.getySpriteScale() + BLOCK_HEIGHT * Locator.getySpriteScale() * y_pos + BLOCK_HEIGHT
						* Locator.getySpriteScale() / 2);
	}

	public void delete() {
		for (Block b : row) {
			b.delete();
		}
	}

	public boolean isBelowScreen() {
		if (!row.isEmpty()) {
			return row.get(0).getPosition().y < (-BLOCK_HEIGHT * Locator.getySpriteScale() * 1.5f * Block.SCALE);
		}
		return true;
	}

	public void update(float delta) {
		for (Block b : row) {
			b.update(delta);
		}
		if (!passedHero && row.get(0).getPosition().y < hero.getPosition().y) {
			passedHero = true;
			subject.notify(hero, Event.EVENT_PASSED_BLOCK);
		} else if (passedHero && row.get(0).getPosition().y >= hero.getPosition().y) {
			passedHero = false;
			subject.notify(hero, Event.EVENT_UNPASSED_BLOCK);
		}
	}

	public void render(SpriteBatch sb) {
		for (Block b : row) {
			b.render(sb);
		}
	}

	public ArrayList<Block> getRow() {
		return row;
	}

	public void setRow(ArrayList<Block> row) {
		this.row = row;
	}

}
